package com.example.j280_webproject.day037;

import java.sql.*;

public class JDBC_ServletXinXi {
    public ResultSet select(String username) throws ClassNotFoundException, SQLException {
        String driver = "com.mysql.jdbc.Driver";   // 固定写法
        Class.forName(driver);                     // 通过反射机制 把指定的类加载到当前类中

        // WARNing
        String url = "jdbc:mysql://localhost:3306/mygxa?useSSL=false&characterEncoding=UTF-8";
        String user = "root";
        String pass = "root";
        Connection conn = DriverManager.getConnection(url, user, pass);    // JDBC数据库连接池
        System.out.println("连接成功");

        String sql="select * from user where username=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1,username);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    public ResultSet select(String username ,String password) throws ClassNotFoundException, SQLException {
        String driver = "com.mysql.jdbc.Driver";   // 固定写法
        Class.forName(driver);                     // 通过反射机制 把指定的类加载到当前类中

        // WARNing
        String url = "jdbc:mysql://localhost:3306/mygxa?useSSL=false&characterEncoding=UTF-8";
        String user = "root";
        String pass = "root";
        Connection conn = DriverManager.getConnection(url, user, pass);    // JDBC数据库连接池
        System.out.println("连接成功");

        String sql="select * from user where username=? and password=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    public ResultSet select(String username ,String password,int age) throws ClassNotFoundException, SQLException {
        String driver = "com.mysql.jdbc.Driver";   // 固定写法
        Class.forName(driver);                     // 通过反射机制 把指定的类加载到当前类中

        // WARNing
        String url = "jdbc:mysql://localhost:3306/mygxa?useSSL=false&characterEncoding=UTF-8";
        String user = "root";
        String pass = "root";
        Connection conn = DriverManager.getConnection(url, user, pass);    // JDBC数据库连接池
        System.out.println("连接成功");

        String sql="select * from user where username =?and password=? and age=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        preparedStatement.setInt(3,age);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    public ResultSet select() throws ClassNotFoundException, SQLException {
        String driver = "com.mysql.jdbc.Driver";   // 固定写法
        Class.forName(driver);                     // 通过反射机制 把指定的类加载到当前类中

        // WARNing
        String url = "jdbc:mysql://localhost:3306/mygxa?useSSL=false&characterEncoding=UTF-8";
        String user = "root";
        String pass = "root";
        Connection conn = DriverManager.getConnection(url, user, pass);    // JDBC数据库连接池
        System.out.println("连接成功");

        String sql="select * from user ";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
}
